<template>
  <div class="view-box">
    <!--背景盒子-->
    <div class="bg-fox">
      <img src="@/assets/images/login-sc4.png"
           style="position: absolute; bottom: 8vh; left: 8vw; width: 450px;">
      <div style="width: 100%; height: 100%">
        <!-- 三角形-->
        <div class="sjx" style="position: absolute; top: 0vh; left: 25vw; transform: rotate(70deg) scale(0.8, 0.8);"/>
        <div class="sjx" style="position: absolute; top: 0vh; right: 15vw; transform: rotate(60deg) scale(0.5, 0.5);"/>
        <div class="sjx" style="position: absolute; top: 25vh; right: -2vw; transform: rotate(40deg);"/>
        <div class="sjx"
             style="position: absolute; bottom: 10vh; right: 20vw; transform: rotate(10deg) scale(1.5, 1.5);"/>
      </div>
    </div>
    <!--内容盒子-->
    <div class="for-box">
      <div class="login-box">
        <div class="login-box-2">
          <!-- 表单盒子 -->
          <div class="from-box" :class="{'from-box-show': show}">
            <h3 class="from-title">
              <img src="@/assets/logo.svg" class="logo">
              <span>{{ title }}</span>
            </h3>
            <el-form autoComplete="on"
                     :model="loginForm"
                     :rules="loginRules"
                     ref="loginForm"
                     label-position="left">
              <el-form-item prop="username">
                <el-input name="username"
                          type="text"
                          v-model="loginForm.username"
                          autoComplete="on"
                          placeholder="请输入用户名">
          <span slot="prefix">
            <svg-icon icon-class="user" class="color-main"></svg-icon>
          </span>
                </el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input name="password"
                          :type="pwdType"
                          @keyup.enter.native="handleLogin"
                          v-model="loginForm.password"
                          autoComplete="on"
                          placeholder="请输入密码">
          <span slot="prefix">
            <svg-icon icon-class="password" class="color-main"></svg-icon>
          </span>
                  <span slot="suffix" @click="showPwd">
            <svg-icon icon-class="eye" class="color-main"></svg-icon>
          </span>
                </el-input>
              </el-form-item>
              <el-form-item style="margin-bottom: 60px;text-align: center">
                <el-form-item>
                  <span style="color: #999;float: left"> <el-checkbox v-model="remember">记住我</el-checkbox></span>
                  <span style="float: right; color: #999;">
                    还没账号？<a @click="handleRegister" style="color:blue">点此注册</a>
                  </span>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" size="medium" style="width: 100%;" @click.native.prevent="handleLogin">
                    登录
                  </el-button>
                </el-form-item>
              </el-form-item>
            </el-form>

          </div>
        </div>
      </div>

      <!-- 底部 版权 -->
      <div style="position: absolute; bottom: 40px; width: 100%; text-align: center; color: #666;">
        Copyright ©2023 {{ title }} | <a href="https://zoz.cool" style="color:blue">zoz.cool</a> - 提供技术支持
      </div>

    </div>

  </div>
</template>

<script>
import {isvalidUsername} from '@/utils/validate';
import {setCookie, getCookie} from '@/utils/support';

export default {
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    };
    const validatePass = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于6位'))
      } else {
        callback()
      }
    };
    return {
      title: "发票管家",
      show: false,
      loginForm: {
        username: '',
        password: '',
      },
      loginRules: {
        username: [{required: true, trigger: 'blur', validator: validateUsername}],
        password: [{required: true, trigger: 'blur', validator: validatePass}]
      },
      loading: false,
      remember: false,
      pwdType: 'password',
      dialogVisible: false,
      supportDialogVisible: false
    }
  },
  mounted() {
    setTimeout(function () {
      this.show = true;
    }.bind(this), 0);
  },
  created() {
    this.loginForm.username = getCookie("username");
    this.loginForm.password = getCookie("password");
    if (this.loginForm.username === undefined || this.loginForm.username == null || this.loginForm.username === '') {
      this.loginForm.username = 'admin';
    }
    if (this.loginForm.password === undefined || this.loginForm.password == null) {
      this.loginForm.password = '';
    }
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false;
            setCookie("username", this.loginForm.username, 15);
            setCookie("password", this.loginForm.password, 15);
            this.$router.push({path: '/'})
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('参数验证不合法！');
          return false
        }
      })
    },
    handleRegister() {
      this.$router.push({path: '/register'});
    }
  }
}
</script>

<style scoped>
/* 视图盒子 */
.view-box {
}

/*  */
.bg-fox {
  position: fixed;
  width: 100%;
  height: 100%;
  /*background-color: #fafeff;*/
  /*background-color: #f8f8ff;*/
  background-color: rgb(250, 254, 255);
  pointer-events: none;
}

.for-box {
  position: fixed;
  width: 100%;
  height: 100%;
  z-index: 10;
  display: flex;
  align-items: center;
}

.login-box {
  flex: 1;
}

.login-box-2 {
  width: 450px;
  max-width: 90vw;
  margin: auto;
  padding-bottom: 100px;
}

/* 表单 */
.from-box {
  padding: 45px 50px 25px;
  background-color: rgba(255, 255, 255, 0.5);
  border: 1px #e5e5e5 solid;
}

.from-box {
  border-radius: 1px; /*box-shadow: 1px 1px 2px #666;*/
}

.from-title {
  font-size: 24px;
  color: #000;
  margin-bottom: 30px;
  text-align: center;
  position: relative;
  left: -15px;
}

.logo {
  width: 50px;
  height: 50px;
  vertical-align: middle;
  margin-right: 15px;
}

/*动画*/
.bg-fox > div {
  animation: changes 30s 0.2s linear infinite alternate;
}

/* normal | alternate */
@keyframes changes {
  from {
    transform: translate(0, 0vh);
  }
  to {
    transform: translate(0, 15vh);
  }
}

.sjx {
  width: 0;
  height: 0;
  border-left: 100px solid transparent;
  border-right: 100px solid transparent;
  border-bottom: 180px solid #dee7f8;
}

/* 2px圆角 */
.view-box >>> .el-input__inner,
.view-box >>> .el-button {
  border-radius: 2px !important;
}

/* 渐渐显示的动画 */
.from-box {
  opacity: 0;
  transition: opacity 1s;
}

.from-box-show {
  opacity: 1;
}

</style>
